<?php
require_once('Kbs/Model/Business/Abstract.php');

class Kbs_Model_Business_Post extends Kbs_Model_Business_Abstract
{
    protected $_mapperClassName = 'Kbs_Model_Mapper_Post';

    protected $_postId;
    protected $_postCategoryId;
    protected $_postAuthorId;
    protected $_postCreatedAt;
    protected $_postUpdatedAt;
    protected $_postUrlTitle;
    protected $_postAdjusted;
    protected $_postPublished;

    /**
     * Post category
     *
     * @var Kbs_Model_Business_Post_Category
     */
    protected $_category = null;

    /**
     * Post detail by current lcoale
     *
     * @var array of Kbs_Model_Business_Post_Detail
     */
    protected $_details = null;

    /**
     * Post detail by current lcoale
     *
     * @var Kbs_Model_Business_Post_Detail
     */
    protected $_detailByLocale = null;

    /**
     * Get id
     */
    public function getId()
    {
        return $this->getPostId();
    }

    public function setId($id)
    {
        return $this->setPostId($id);
    }

    /**
     * Get id
     */
    public function getPostId()
    {
        return (int)$this->_postId;
    }

    public function setPostId($id)
    {
        $this->_postId = (int)$id;
        return $this;
    }

    /**
     * Get category id
     */
    public function getPostCategoryId()
    {
        return (int)$this->_postCategoryId;
    }

    public function setPostCategoryId($id)
    {
        $this->_postCategoryId = (int)$id;
        return $this;
    }

    /**
     * Get author id
     */
    public function getPostAuthorId()
    {
        return (int)$this->_postAuthorId;
    }

    public function setPostAuthorId($id)
    {
        $this->_postAuthorId = (int)$id;
        return $this;
    }

    /**
     * Get post create date
     */
    public function getPostCreatedAt()
    {
        return (string)$this->_postCreatedAt;
    }

    public function setPostCreatedAt($string)
    {
        $this->_postCreatedAt = (string)$string;
        return $this;
    }

    /**
     * Get post create date
     */
    public function getPostUpdatedAt()
    {
        return (string)$this->_postUpdatedAt;
    }

    public function setPostUpdatedAt($string)
    {
        $this->_postUpdatedAt = (string)$string;
        return $this;
    }

    /**
     * Get locale title
     */
    public function getPostUrlTitle()
    {
        return (string)$this->_postUrlTitle;
    }

    public function setPostUrlTitle($string)
    {
        $this->_postUrlTitle = (string)$string;
        return $this;
    }

    /**
     * Get adjusted sign
     */
    public function getPostAdjusted()
    {
        return (bool)$this->_postAdjusted ? 1 : 0;
    }

    public function setPostAdjusted($isAdjusted)
    {
        if ((bool)$isAdjusted == true) {
            $this->_postAdjusted = 1;
        } else {
            $this->_postAdjusted = 0;
        }
        return $this;
    }

    /**
     * Get published sign
     */
    public function getPostPublished()
    {
        return (bool)$this->_postPublished ? 1 : 0;
    }

    public function setPostPublished($isPublished)
    {
        if ((bool)$isPublished == true) {
            $this->_postPublished = 1;
        } else {
            $this->_postPublished = 0;
        }
        return $this;
    }

    /**
     * Fetch the category for the post
     *
     * @return Kbs_Model_Business_Post_Category
     */
    public function fetchCategory()
    {
        if (null == $this->_category) {
            $this->_category = new Kbs_Model_Business_Post_Category();
            $this->_category->find($this->postCategoryId);
        }
        return $this->_category;
    }

    /**
     * Fetch details for the post
     *
     *
     */
    public function fetchDetails()
    {
        if (null == $this->_details) {
            $detailObj = new Kbs_Model_Business_Post_Detail();
            $this->_details = $detailObj->findByDetailPostId($this->postId);
        }
        return $this->_details;
    }

    /**
     * Fetch detail for the post by current locale
     *
     * @return Kbs_Model_Business_Post_Detail|false
     */
    public function fetchDetailByLocale($localeId = null)
    {
        //if (null == $this->_detailByLocale) {
        if (null == $localeId) {
            $session = new Zend_Session_Namespace('locale');
            $localeId = (int)$session->localeId;
        }
        $detailObj = new Kbs_Model_Business_Post_Detail();
        $detail = $detailObj->findByParams(array('detailPostId' => $this->postId, 
                                                 'detailLocaleId' => $localeId));
        $this->_detailByLocale = $detail[0];
        //}
        return $this->_detailByLocale;
    }

    /**
     * Get current date
     *
     */
    static public function datetime()
    {
        return date('Y-m-d H:i:s');
    }

    /**
     * Get the posts list by locale awared for F.O.
     *
     * @return array
     */
    public function getPostsListByLocale($postCategoryId = null, $postId = null)
    {
        if (isset($postId)) {
            //$posts = array($this->find($postId));
            $posts = $this->findByParams(array('postId' => $postId,
                                               'postPublished' => 1));
        } elseif (isset($postCategoryId)) {
            //$posts = $this->findByPostCategoryId($postCategoryId);
            $posts = $this->findByParams(array('postCategoryId' => $postCategoryId,
                                               'postPublished' => 1));
        } else {
            //$posts = $this->fetchAll();
            $posts = $this->findByPostPublished(1);
        }

        //var_dump($posts);die;

        $result = array();
        if (!empty($posts)) {
            foreach ($posts as $post) {

                // If we can't get the details then we ignore it !
                if ($post->fetchDetailByLocale()) {
                    $title = trim($post->fetchDetailByLocale()->detailTitle);
                    $content = trim($post->fetchDetailByLocale()->detailContent);
                    if (!empty($title) and !empty($content)) {
                        $result[] = $post;
                    }
                }
            }
            // Order DESC : we need the lastest to be posted first
            $result = array_reverse($result);
        }

        //var_dump($result);
        //die;
        return $result;
    }

    /**
     * Save the post and it's details
     *
     * @param array $data
     */
    public function savePostAndDetails($data)
    {
        $data['postCreatedAt'] = !empty($data['postCreatedAt'])
                               ? $data['postCreatedAt']
                               : self::datetime();

        $data['postUpdatedAt'] = !empty($data['postUpdatedAt'])
                               ? $data['postUpdatedAt']
                               : self::datetime();

        $this->setPostId($data['postId'])
             ->setPostCategoryId($data['postCategoryId'])
             ->setPostAuthorId($data['postAuthorId'])
             ->setPostCreatedAt($data['postCreatedAt'])
             ->setPostUpdatedAt($data['postUpdatedAt'])
             ->setPostUrlTitle($data['postUrlTitle'])
             ->setPostAdjusted($data['postAdjusted'])
             ->setPostPublished($data['postPublished'])
             ;

        //var_dump($data);die;

        // $newId is the identity (primary key)
        $newId = $this->save();

        $localeObj = new Kbs_Model_Business_Locale();
        foreach ($localeObj->fetchAll() as $locale) {

            $postDetailObj = new Kbs_Model_Business_Post_Detail();
            $postDetailObj->setDetailId($data['detailId'][$locale->localeId])
                          ->setDetailPostId($newId)
                          ->setDetailLocaleId($locale->localeId)
                          ->setDetailTitle($data['detailTitle'][$locale->localeId])
                          ->setDetailContent($data['detailContent'][$locale->localeId]);
            $postDetailObj->save();
        }
    }

    /**
     * Delete the post
     *
     */
    public function deletePostAndDetails($id)
    {
        $this->find($id);
        $this->delete();

        $detail = new Kbs_Model_Business_Post_Detail();
        $details = $detail->findByDetailPostId($id);
        if (!empty($details)) {
            foreach ($details as $detail) {
                $detail->delete();
            }
        }
    }

}